package com.sena.service.imp;

import com.sena.domain.SenaSysUser;
import com.sena.enums.ResultCodeEnum;
import com.sena.exception.ThreadRunTimeException;
import com.sena.mapper.SenaSysUserMapper;
import com.sena.service.SenaSysUserService;
import com.sena.utils.AssertUtils;
import com.sena.utils.BaseUtils;
import com.sena.utils.MD5Gen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Admin
 */
@Service
public class SenaSysUserServiceImp implements SenaSysUserService {


    @Autowired
    private SenaSysUserMapper senaSysUserMapper;

    @Override
    public SenaSysUser login(SenaSysUser senaSysUser, HttpServletRequest request) {
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), senaSysUser);
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), senaSysUser.getmName());
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK), senaSysUser.getmPwd());
        senaSysUser.setmPwd(MD5Gen.getMD5(senaSysUser.getmPwd()));
        senaSysUser.setmLoginDate(new Date());
        senaSysUser.setmLoginIp(BaseUtils.getIpAddr(request));
        SenaSysUser ret = senaSysUserMapper.selectByNameAndPwd(senaSysUser);
        AssertUtils.isNotNull(new ThreadRunTimeException(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "用户不存在"), ret);
        senaSysUser.setmLoginDate(new Date());
        senaSysUser.setmPerIp(ret.getmLoginIp());
        senaSysUser.setmLoginDate(new Date());
        senaSysUser.setPkGuid(ret.getPkGuid());
        senaSysUserMapper.updateByPrimaryKeySelective(senaSysUser);
        ret.setmLoginIp(BaseUtils.getIpAddr(request));
        return ret;

    }
}
